package com.hnas.trade.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hnas.common.service.dto.search.DTOCommoditOrderSearch;
import com.hnas.common.service.dto.search.DTOECardOrderSearch;
import com.hnas.common.service.dto.search.DTOOrderSearch;
import com.hnas.core.dto.DTOPagedList;
import com.hnas.core.dto.DataBean;
import com.hnas.ecard.domain.CommoditOrderStatus;
import com.hnas.ecard.orm.SaleCardOrderDetail;
import com.hnas.ecard.service.CommoditOrderDetailService;
import com.hnas.ecard.service.SaleCardOrderDetailService;
import com.hnas.ecard.service.dto.DTOCommoditOrder;
import com.hnas.ecard.service.dto.DTOCommoditOrderDetail;
import com.hnas.ecard.service.dto.DTOECardOrder;
import com.hnas.trade.domain.OrderStatus;
import com.hnas.trade.domain.OrderType;
import com.hnas.trade.domain.PayStatus;
import com.hnas.trade.service.OrderDetailService;
import com.hnas.trade.service.OrderService;
import com.hnas.trade.service.dto.DTOOrder;
import com.hnas.trade.service.dto.DTOOrderDetail;

@Controller
@RequestMapping("/backend/order")
public class OrderController {
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private OrderDetailService orderDetailService;
	
	@Autowired
	private SaleCardOrderDetailService saleCardOrderDetailService;
	@Autowired
	private CommoditOrderDetailService commoditOrderDetailService;

	@RequestMapping("/searchEntityOrder")
	public String searchEntityOrder(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("criteria")DTOOrderSearch criteria){
		criteria.setTotalCount(-1);
		criteria.setOrderType(OrderType.STKGK.getType());
		DTOPagedList<DTOOrder> pg = this.orderService.search(criteria);
		List<DataBean> beans = new ArrayList<DataBean>();
		beans.add(new DataBean("orderNo","orderNo","订单号",false,150,0));
		beans.add(new DataBean("orderTypeName","orderTypeName","订单类型",false,150,0));
		beans.add(new DataBean("orderStatusName","orderStatusName","订单状态",false,150,0));
		beans.add(new DataBean("payStatusName","payStatusName","支付状态",false,150,0));
		beans.add(new DataBean("needPayDouble","needPayDouble","应付金额",false,150,0));
		beans.add(new DataBean("realPayDouble","realPayDouble","实付金额",false,150,0));
		beans.add(new DataBean("swiftNumber","swiftNumber","微信订单号",false,150,0));
		beans.add(new DataBean("mobilePhone","mobilePhone","手机号",false,150,0));
		beans.add(new DataBean("createTime","createTime","创建时间",false,150,0));
		beans.add(new DataBean("tradeEndTime","tradeEndTime","交易时间",false,150,0));
		
		//明细附加显示
		beans.add(new DataBean("accId","accId","用户ID",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("accName","accName","用户名称",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("swiftNumber","swiftNumber","swiftNumber",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("commidityId","commidityId","商品ID",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("commidityType","commidityType","商品类型",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("commidityName","commidityName","商品名称",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("psamId","psamId","psamId",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("subId","subId","科目号",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("payEndTime","payEndTime","支付时间",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("cancelTime","cancelTime","订单取消时间",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("updateTime","updateTime","修改时间",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("updater","updater","修改人",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("remark","remark","备注",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("qrcodeNo","qrcodeNo","二维码标识",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("orderParentType","orderParentType","大类类型",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("beforeTradeBalance","beforeTradeBalance","交易前余额",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("afterTradeBalance","afterTradeBalance","交易后余额",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("updateTime","updateTime","修改时间",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("updater","updater","修改人",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("remark","remark","备注",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		beans.add(new DataBean("qrcodeNo","qrcodeNo","二维码标识",false,150,0));
		beans.get(beans.size() - 1).setDisplay(false);
		
		modelMap.put("pg", pg);
		modelMap.put("dto", criteria);
		modelMap.put("beans", beans);
		modelMap.put("orderStatusMap", OrderStatus.getOrderStatusMap());
		modelMap.put("payStatusMap", PayStatus.getPayStatusMap());
		
		return "/backend/ecard/list_entity_order";
	}
	
	@RequestMapping("/searchECardOrder")
	public String searchECardOrder(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("criteria")DTOECardOrderSearch criteria){
		criteria.setTotalCount(-1);
		criteria.setOrderType(OrderType.DZKGK.getType());
		DTOPagedList<DTOECardOrder> pg = this.orderService.searchECardOrder(criteria);
		List<DataBean> beans = new ArrayList<DataBean>();
		beans.add(new DataBean("orderNo","orderNo","订单编号",false,150,0));
		beans.add(new DataBean("swiftNumber","swiftNumber","微信订单号",false,150,0));
		beans.add(new DataBean("mobilePhone","mobilePhone","手机号",false,150,0));
		beans.add(new DataBean("orderTypeName","orderTypeName","订单类型",false,150,0));
		beans.add(new DataBean("orderStatusName","orderStatusName","订单状态",false,150,0));
		beans.add(new DataBean("payStatusName","payStatusName","支付状态",false,150,0));
		beans.add(new DataBean("needPayDouble","needPayDouble","应付金额",false,150,0));
		beans.add(new DataBean("realPayDouble","realPayDouble","实付金额",false,150,0));
		beans.add(new DataBean("createTime","createTime","订单创建时间",false,150,0));
		beans.add(new DataBean("tradeEndTime","tradeEndTime","交易完成时间",false,150,0));
		modelMap.put("pg", pg);
		modelMap.put("dto", criteria);
		modelMap.put("beans", beans);
		modelMap.put("orderStatusMap", OrderStatus.getOrderStatusMap());
		modelMap.put("payStatusMap", PayStatus.getPayStatusMap());
		
		return "/backend/ecard/list_ecard_order";
	}
	
	@RequestMapping("/searchCommoditOrder")
	public String searchCommoditOrder(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("criteria")DTOCommoditOrderSearch criteria){
		criteria.setTotalCount(-1);
		criteria.setOrderType(OrderType.GMSP.getType());
		DTOPagedList<DTOCommoditOrder> pg = this.orderService.searchCommoditOrder(criteria);
		List<DataBean> beans = new ArrayList<DataBean>();
		beans.add(new DataBean("orderNo","orderNo","订单编号",false,150,0));
		beans.add(new DataBean("swiftNumber","swiftNumber","微信订单号",false,150,0));
		beans.add(new DataBean("mobilePhone","mobilePhone","手机号",false,150,0));
		beans.add(new DataBean("stateStr","stateStr","核销状态",false,150,0));
		beans.add(new DataBean("orderTypeName","orderTypeName","订单类型",false,150,0));
		beans.add(new DataBean("orderStatusName","orderStatusName","订单状态",false,150,0));
		beans.add(new DataBean("payStatusName","payStatusName","支付状态",false,150,0));
		beans.add(new DataBean("needPayDouble","needPayDouble","应付金额",false,150,0));
		beans.add(new DataBean("realPayDouble","realPayDouble","实付金额",false,150,0));
		beans.add(new DataBean("createTime","createTime","订单创建时间",false,150,0));
		beans.add(new DataBean("tradeEndTime","tradeEndTime","交易完成时间",false,150,0));
		modelMap.put("pg", pg);
		modelMap.put("dto", criteria);
		modelMap.put("beans", beans);
		modelMap.put("stateMap", CommoditOrderStatus.getMap());
		modelMap.put("orderStatusMap", OrderStatus.getOrderStatusMap());
		modelMap.put("payStatusMap", PayStatus.getPayStatusMap());
		
		return "/backend/ecard/list_commodit_order";
	}
	
	@RequestMapping("/orderByOrderNo")
	@ResponseBody
	public DTOOrder getOrderByOrderNo(String orderNo){
		return orderService.getByOrderNo(orderNo);
	}
	
	
	@RequestMapping("/searchECardOrderDetail")
	public String searchECardOrderDetail(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("criteria")DTOECardOrderSearch criteria){
		criteria.setTotalCount(-1);
		DTOPagedList<DTOOrderDetail> pg = 	new DTOPagedList<DTOOrderDetail>();
		List<DataBean> beans = new ArrayList<DataBean>();
		SaleCardOrderDetail saleDto = saleCardOrderDetailService.findByOrderNo(criteria.getOrderNo());
		if(null != saleDto){
			DTOOrderDetail orderDetail = new DTOOrderDetail();
			orderDetail.setSaleCount(saleDto.getSaleCount()+"");
			orderDetail.setDenomination(saleDto.getDenomination()+"");
			orderDetail.setDenominationStr(((double)saleDto.getDenomination())/100+"");
			orderDetail.setEcardNos(saleDto.getCards());
			pg.getList().add(orderDetail);
			pg.setPageNo(criteria.getPageNo());
			pg.setPageSize(criteria.getPageSize());
			beans.add(new DataBean("saleCount","saleCount","售卡数量",false,150,0));
			beans.add(new DataBean("denominationStr","denominationStr","卡面额",false,150,0));
			beans.add(new DataBean("ecardNos","ecardNos","卡号",false,150,0));
		}
		modelMap.put("pg", pg);
		modelMap.put("dto", criteria);
		modelMap.put("beans", beans);
		return "/backend/ecard/list_order_detail1";
	}
	
	@RequestMapping("/searchCommoditOrderDetail")
	public String searchCommoditOrderDetail(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("criteria")DTOCommoditOrderSearch criteria){
		criteria.setTotalCount(-1);
		DTOPagedList<DTOCommoditOrderDetail> pg = 	new DTOPagedList<DTOCommoditOrderDetail>();
		List<DataBean> beans = new ArrayList<DataBean>();

		List<DTOCommoditOrderDetail> commoditDetail = commoditOrderDetailService.listByOrderNo(criteria.getOrderNo());
		if(null != commoditDetail){
			pg.setList(commoditDetail);
			pg.setPageNo(criteria.getPageNo());
			pg.setPageSize(criteria.getPageSize());
			beans.add(new DataBean("mrchId","mrchId","核销商户编号",false,150,0));
			beans.add(new DataBean("mrchName","mrchName","核销商户名称",false,150,0));
			beans.add(new DataBean("termNo","termNo","核销终端",false,150,0));
			beans.add(new DataBean("termSno","termSno","设备流水号",false,150,0));
			beans.add(new DataBean("stateStr","stateStr","核销状态",false,150,0));
			beans.add(new DataBean("updateTime","updateTime","核销时间",false,150,0));
			beans.add(new DataBean("orderDetail","orderDetail","详情",false,150,0));
		}
		modelMap.put("pg", pg);
		modelMap.put("dto", criteria);
		modelMap.put("beans", beans);
		return "/backend/ecard/list_order_detail2";
	}
	
	@RequestMapping("/searchDetail")
	public String searchOrderDetail(ModelMap modelMap,HttpServletRequest request,HttpServletResponse response,@ModelAttribute("criteria")DTOOrderSearch criteria){
		criteria.setTotalCount(-1);
		if(OrderType.DZKGK.getType().equals(criteria.getOrderType())){
			//电子卡
			DTOPagedList<DTOOrderDetail> pg = 	new DTOPagedList<DTOOrderDetail>();
			List<DataBean> beans = new ArrayList<DataBean>();
			SaleCardOrderDetail saleDto = saleCardOrderDetailService.findByOrderNo(criteria.getOrderNo());
			if(null != saleDto){
				DTOOrderDetail orderDetail = new DTOOrderDetail();
				orderDetail.setSaleCount(saleDto.getSaleCount()+"");
				orderDetail.setDenomination(saleDto.getDenomination()+"");
				orderDetail.setDenominationStr(((double)saleDto.getDenomination())/100+"");
				orderDetail.setEcardNos(saleDto.getCards());
				pg.getList().add(orderDetail);
				pg.setPageNo(criteria.getPageNo());
				pg.setPageSize(criteria.getPageSize());
				beans.add(new DataBean("saleCount","saleCount","售卡数量",false,150,0));
				beans.add(new DataBean("denominationStr","denominationStr","卡面额",false,150,0));
				beans.add(new DataBean("ecardNos","ecardNos","卡号",false,150,0));
			}
			modelMap.put("pg", pg);
			modelMap.put("dto", criteria);
			modelMap.put("beans", beans);
			return "/backend/ecard/list_order_detail1";
		}else if(OrderType.STKGK.getType().equals(criteria.getOrderType())) {
			//TODO 列出电子卡详情
		}else if(OrderType.GMSP.getType().equals(criteria.getOrderType())) {
			//TODO 列出商品购买详情
			DTOPagedList<DTOCommoditOrderDetail> pg = 	new DTOPagedList<DTOCommoditOrderDetail>();
			List<DataBean> beans = new ArrayList<DataBean>();

			List<DTOCommoditOrderDetail> commoditDetail = commoditOrderDetailService.listByOrderNo(criteria.getOrderNo());
			if(null != commoditDetail){
				pg.setList(commoditDetail);
				pg.setPageNo(criteria.getPageNo());
				pg.setPageSize(criteria.getPageSize());
				beans.add(new DataBean("mrchId","mrchId","核销商户",false,150,0));
				beans.add(new DataBean("termNo","termNo","核销终端",false,150,0));
				beans.add(new DataBean("termSno","termSno","设备流水号",false,150,0));
				beans.add(new DataBean("stateStr","stateStr","核销状态",false,150,0));
				beans.add(new DataBean("orderDetail","orderDetail","详情",false,150,0));
			}
			modelMap.put("pg", pg);
			modelMap.put("dto", criteria);
			modelMap.put("beans", beans);
			return "/backend/ecard/list_order_detail2";
		}
		/*else{
			pg = this.orderDetailService.search(criteria);
			beans.add(new DataBean("orderNo","orderNo","订单号",false,150,0));
			beans.add(new DataBean("typeStr","typeStr","类型名称",false,150,0));
			beans.add(new DataBean("relateId","relateId","关联ID",false,150,0));
			beans.add(new DataBean("discountAmount","discountAmount","打折金额",false,150,0));
			beans.add(new DataBean("couponTypeName","couponTypeName","优惠券名称",false,150,0));
			beans.add(new DataBean("validBeginTime","validBeginTime","有效开始时间",false,150,0));
			beans.add(new DataBean("validBeginTime","validBeginTime","有效结束时间",false,150,0));
			beans.add(new DataBean("productName","productName","产品名称",false,150,0));
			beans.add(new DataBean("productPrice","productPrice","产品价格",false,150,0));
			beans.add(new DataBean("discountTypeStr","discountTypeStr","折扣类型",false,150,0));
			beans.add(new DataBean("activityItemType","activityItemType","活动项类型",false,150,0));
			beans.add(new DataBean("createTime","createTime","创建时间",false,150,0));
		}*/
		return null;
	}
	
	@RequestMapping("/cancelOrder/{orderNo}")
	@ResponseBody
	public String cancelOrder(@PathVariable("orderNo") String orderNo){
		this.commoditOrderDetailService.cancelOrder(orderNo);
		return null;
	}
}
